package com.erpanderp.wam.usm.view.timesheet;

import java.text.DateFormatSymbols;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import com.erpanderp.wam.usm.model.entity.projectandassignment.EmployeeProject;
import com.erpanderp.wam.usm.model.entity.projectandassignment.Project;
import com.erpanderp.wam.usm.model.entity.timesheet.ProjectAssignmentTimesheetDetail;
import com.erpanderp.wam.usm.model.entity.timesheet.Timesheet;
import com.erpanderp.wam.usm.model.entity.timesheet.TimesheetDetail;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.entity.user.EmployeeAndReporting;
import com.erpanderp.wam.usm.model.service.leave.EmployeeLeaveDAO;
import com.erpanderp.wam.usm.model.service.projectandassignment.EmployeeProjectDAO;
import com.erpanderp.wam.usm.model.service.timesheet.TimesheetDAO;
import com.erpanderp.wam.usm.model.service.user.EmployeeAndReportingDAO;
import com.erpanderp.wam.usm.model.service.user.EmployeeDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.WAMUtil;

@ManagedBean
@ViewScoped
public class EmployeeTimesheetCronJobBean {
	String monthNames[] = new DateFormatSymbols().getMonths();
	private String year;
	Calendar calendar = Calendar.getInstance();
	private String timesheetMonth; // actual timesheet month
	private List<EmployeeAndReporting> employeeAndReportingList = new ArrayList<EmployeeAndReporting>();
	private List<EmployeeAndReporting> allEmployeeAndReportingList = new ArrayList<EmployeeAndReporting>();
	String dayNames[] = new DateFormatSymbols().getWeekdays();
	private Timesheet timesheet = new Timesheet();
	private WAMUtil wamUtil = new WAMUtil();
	private List<Timesheet> timesheetsList = new ArrayList<Timesheet>();
	private String cronName;
	private boolean IN_PROJECT;
	EmployeeProjectDAO employeeProjectDAO = new EmployeeProjectDAO();
	TimesheetDAO timesheetDAO = new TimesheetDAO();
	private String employeeId;
	
	
	public EmployeeTimesheetCronJobBean(){
		
	}
	
	
	
	
	public void generateEmployeeTimesheet() {

		if (calendar.get(Calendar.DAY_OF_MONTH) > 24) {
			calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
		}

		timesheetMonth = monthNames[calendar.get(Calendar.MONTH)].toUpperCase();
		year = String.valueOf(calendar.get(Calendar.YEAR));
		allEmployeeAndReportingList = new EmployeeAndReportingDAO().getAllEmployeesAndReportings();
		for (EmployeeAndReporting employeeAndReporting : allEmployeeAndReportingList) {
			if (employeeAndReporting.getEmployee().getEmployeeCode().equals("admin") || employeeAndReporting.getEmployee().getEmployeeCode().equals("adminhr") || employeeAndReporting.getEmployee().getEmployeeCode().equals("A00001")) {
				System.out.println("ADMIN && ADMIN HR FOUND");
			} else {
                System.out.println("EMPLOYEE ID = "+employeeId);
				if(employeeAndReporting.getEmployee().getEmployeeCode().equals(employeeId)){	
					timesheet = new Timesheet();
					List<TimesheetDetail> timesheetDetails = new LinkedList<TimesheetDetail>();
					timesheet.setApprover(employeeAndReporting.getProjectManager());
					timesheet.setEmployee(employeeAndReporting.getEmployee());
					timesheet.setMonth(timesheetMonth);
					timesheet.setYear(Integer.parseInt(year));
					timesheet.setTimesheetStatus(CommonLookup.TIMESHEET_STATUS_NOT_SUBMITTED);
					timesheet.setStatus(CommonLookup.STATUS_ACTIVE);
					// Generating...

					Date startDate = null;
					Date endDate = null;
					endDate = wamUtil.getTimesheetEndDate(timesheetMonth);
					Calendar temp = Calendar.getInstance();
					temp.setTime(endDate);
					startDate = wamUtil.getTimesheetStartDate(temp);
					Map<Date, String> leavesMap = new EmployeeLeaveDAO()
							.getApprovedEmployeeLeaveDates(startDate, endDate,
									employeeAndReporting.getEmployee().getEmployeeId());

					while (startDate.before(endDate) || startDate.equals(endDate)) {
						Calendar calendar = Calendar.getInstance();
						calendar.setTime(startDate);

						TimesheetDetail timesheetDetail = new TimesheetDetail();
						timesheetDetail.setDuration("09:00");
						timesheetDetail.setInTimeString("09:30");
						timesheetDetail.setOutTimeString("18:30");
						timesheetDetail.setTimesheetDate(startDate);
						timesheetDetail.setDayOfWeek(dayNames[calendar
								.get(Calendar.DAY_OF_WEEK)]);
						if (timesheetDetail.getDayOfWeek().equalsIgnoreCase("Saturday")
								|| timesheetDetail.getDayOfWeek().equalsIgnoreCase(
										"Sunday")) {
							timesheetDetail.setActivity(CommonLookup.TEXT_WEEK_OFF);
						}

						List<Project> projects = employeeProjectDAO.getAllProjects(employeeAndReporting.getEmployee().getEmployeeId());
						if (employeeAndReporting.getProject() != null && !employeeAndReporting.getProject().equals(CommonLookup.PROJECT_KA) && projects != null  && projects.size() > 0) {
							IN_PROJECT = true;
						}

						if (IN_PROJECT) {
							List<EmployeeProject> employeeProjects = employeeProjectDAO.getAllEmployeeProjects(employeeAndReporting.getEmployee().getEmployeeId());

							if (employeeProjects != null && employeeProjects.size() > 0) {
								for (EmployeeProject employeeProject : employeeProjects) {
									ProjectAssignmentTimesheetDetail assignmentTimesheetDetail = new ProjectAssignmentTimesheetDetail();
									assignmentTimesheetDetail.setEmployeeProject(employeeProject);
									assignmentTimesheetDetail.setTimeSpent("00.00");
									assignmentTimesheetDetail.setTimesheetDetail(timesheetDetail);
									timesheetDetail.getProjectAssignmentTimesheetDetails().add(assignmentTimesheetDetail);
									employeeProject.getProjectAssignmentTimesheetDetails().add(assignmentTimesheetDetail);
								}
							}
						}

						// Other Attributes...
						timesheetDetail.setCreateDate(new Date());

						timesheetDetail.setStatus(CommonLookup.STATUS_ACTIVE);
						timesheetDetail.setTimesheet(timesheet);

						System.out.println("startdate: " + startDate);
						// Set the timesheetdetailstatus based on leave applied.
						if(leavesMap != null && leavesMap.containsKey(startDate)){
							timesheetDetail.setTimesheetDetailStatus(leavesMap.get(startDate));
						}
						
						timesheetDetails.add(timesheetDetail);

						calendar.add(Calendar.DAY_OF_MONTH, 1);
						startDate = calendar.getTime();

					}

					timesheet.getTimesheetDetails().addAll(timesheetDetails);

					timesheetsList.add(timesheet);
				
				}
			}
		}
		new TimesheetDAO().createTimesheetsForWamAllEmployees(timesheetsList);
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public String getTimesheetMonth() {
		return timesheetMonth;
	}

	public void setTimesheetMonth(String timesheetMonth) {
		this.timesheetMonth = timesheetMonth;
	}

	public List<EmployeeAndReporting> getEmployeeAndReportingList() {
		return employeeAndReportingList;
	}

	public void setEmployeeAndReportingList(
			List<EmployeeAndReporting> employeeAndReportingList) {
		this.employeeAndReportingList = employeeAndReportingList;
	}

	public List<Timesheet> getTimesheetsList() {
		return timesheetsList;
	}

	public void setTimesheetsList(List<Timesheet> timesheetsList) {
		this.timesheetsList = timesheetsList;
	}

	public String getCronName() {
		return cronName;
	}

	public void setCronName(String cronName) {
		this.cronName = cronName;
	}

	public static void main(String[] args) {

		EmployeeTimesheetCronJobBean timesheetCronJobBean = new EmployeeTimesheetCronJobBean();

	}

	public boolean isIN_PROJECT() {
		return IN_PROJECT;
	}

	public void setIN_PROJECT(boolean iN_PROJECT) {
		IN_PROJECT = iN_PROJECT;
	}

	public List<EmployeeAndReporting> getAllEmployeeAndReportingList() {
		return allEmployeeAndReportingList;
	}

	public void setAllEmployeeAndReportingList(
			List<EmployeeAndReporting> allEmployeeAndReportingList) {
		this.allEmployeeAndReportingList = allEmployeeAndReportingList;
	}

	public String getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(String employeeId) {
		this.employeeId = employeeId;
	}

}
